package com.demo.controller;


import com.demo.bean.User;
import com.demo.common.Result;
import com.demo.service.PieceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("piece")
public class PieceController {
    @Autowired
    PieceService service;


    /**
     * 分页查询
     *
     * @param筛选条件

     * @return 查询结果
     */
    @RequestMapping("getPageList")
    public Result getPageList(@RequestBody Map param, HttpServletRequest request) {
        //获取当前用户
        User user= (User) request.getSession().getAttribute("user");
        param.put("userId",user.getId());
        int pageNum = (int) param.get("page");
        int limitNum = (int) param.get("limit");
        pageNum = (pageNum-1) * limitNum ;

        param.put("page", pageNum);
        param.put("limit", limitNum);
        System.out.println(param);
        Map<String, Object> pageList = service.getPageList(param);
        return Result.ok().Data(pageList);
    }

    @RequestMapping("/saveItem")
    @ResponseBody
    public Result saveItem(@RequestBody Map param,HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("user");
        param.put("userId",user.getId());
        service.saveItem(param);
        return Result.ok();
    }

    /**
     * 预览音频
     * @param param
     * @return
     */
    @RequestMapping("/preview")
    @ResponseBody
    public Result preview(@RequestBody Map param){
        String name = service.preview(param);
        return Result.ok().Data(name);
    }

    @RequestMapping("/getAll")
    @ResponseBody
    public Result getAll(@RequestParam(value = "userId")String userId){
        List<Map> data= service.getAll(userId);
        return Result.ok().Data(data);
    }



    @RequestMapping("/getById")
    @ResponseBody
    public Result getById(@RequestParam("id")String id){
        User user= service.getById(id);
        return Result.ok().Data(user);
    }

    @RequestMapping("/deleteItemsByIds")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Result deleteItemsByIds(@RequestBody Map param){
        service.deleteItemsByIds(param);
        return Result.ok();
    }



}
